package hms.rooms;


import hms.database.DBRowObject;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;


public class Room extends DBRowObject {

	private String roomLabel;
	private String roomTypeLabel;
	private Integer reservationID;
	private String occupationStatus;
	private int floor;
	private String additionalNotes;
	private String decommissionReason;
	private Date decommissionDateComplete;
	private String connectedRoomLabel;
	
	public Room(){
		super();
		reservationID = -1;
		roomLabel = "rm-not-valid-label";
		roomTypeLabel = "Default";
		occupationStatus = "Empty";
		floor = 1;
		additionalNotes = null;
		decommissionReason = null;
		decommissionDateComplete = null;
		connectedRoomLabel = null;
	}
	
	public Room(int roomID){
		super(roomID);
	}
	
	public Room(String i_roomLabel, String i_roomTypeLabel){
		this(i_roomLabel, i_roomTypeLabel, "Empty", 1);
	}
	
	public Room(String i_roomLabel, String i_roomTypeLabel, String i_occupationStatus, int i_floor){
		this();
		setRoomLabel(i_roomLabel);
		setRoomTypeLabel(i_roomTypeLabel);
		setOccupationStatus(i_occupationStatus);
		setFloorNumber(i_floor);
	}
	
	public void setRoomLabel(String i){
		roomLabel = i;
		markAsChanged();
	}
	
	public void setRoomTypeLabel(String i){
		roomTypeLabel = i;
		markAsChanged();
	}
	
	public void setOccupationStatus(String i){
		occupationStatus = i;
		markAsChanged();
	}
	
	public void setFloorNumber(int i){
		floor = i;
		markAsChanged();
	}
	
	// TODO: [Matt] This will not work for setting reservations, so fix this
	public void setReservationID(Integer i){
		reservationID = i;
		markAsChanged();
	}
	
	public void setAdditionalNotes(String i){
		additionalNotes = i;
		markAsChanged();
	}
	
	public void setDecommissionReason(String i){
		decommissionReason = i;
		markAsChanged();
	}
	
	public void setDecommissionDateComplete(Date i){
		decommissionDateComplete = i;
		markAsChanged();
	}
	
	public void setConnectedRoomLabel(String i){
		connectedRoomLabel = i;
		markAsChanged();
	}
	
	public int getPricePerNight(){

		RoomTypeTable rtt = new RoomTypeTable();
		RoomType roomType = rtt.getRoomTypeByLabel(roomTypeLabel);

		return roomType.getPricePerNight();
	}
	public String getRoomLabel(){
		return roomLabel;
	}
	
	public String getRoomTypeLabel(){
		return roomTypeLabel;
	}
	
	public String getOccupationStatus(){
		return occupationStatus;
	}
	
	public int getFloorNumber(){
		return floor;
	}
	
	public int getReservationID(){
		return reservationID;
	}
	
	public String getAdditionalNotes(){
		return additionalNotes;
	}
	
	public String getDecommissionReason(){
		return decommissionReason;
	}
	
	public Date getDecommissionDateComplete(){
		return decommissionDateComplete;
	}
	
	public String getConnectedRoomLabel(){
		return connectedRoomLabel;
	}

	@Override
	protected String[] getFieldNames() {
		String[] out = new String[8];
		out[0] = "ROOMLABEL";
		out[1] = "ROOMTYPELABEL";
		out[2] = "OCCUPATIONSTATUS";
		out[3] = "FLOOR";
		out[4] = "ADDITIONALNOTES";
		out[5] = "DECOMMISSIONREASON";
		out[6] = "DECOMISSIONDATECOMPLETE";
		out[7] = "CONNECTEDROOM";
		return out;
	}

	@Override
	protected void loadPreparedStatement(PreparedStatement stmt)
			throws SQLException {
		stmt.setString(1, roomLabel);
		stmt.setString(2, roomTypeLabel);
		stmt.setString(3, occupationStatus);
		stmt.setInt(4, floor);
		stmt.setString(5, additionalNotes);
		stmt.setString(6, decommissionReason);
		stmt.setDate(7, (java.sql.Date) decommissionDateComplete);
		stmt.setString(8, connectedRoomLabel);

	}

	@Override
	protected String getUniqueIdentifierFieldName() {
		return "ROOMID"; 
	}

	@Override
	protected String getTableName() {
		return "ROOM";
	}

	@Override
	protected void loadFromDatabase(ResultSet rs) throws SQLException {
		this.roomLabel = rs.getString("ROOMLABEL");
		this.roomTypeLabel = rs.getString("ROOMTYPELABEL");
		this.reservationID = rs.getInt("RESERVATION_ID");
		this.occupationStatus = rs.getString("OCCUPATIONSTATUS");
		this.floor = rs.getInt("FLOOR");
		this.additionalNotes = rs.getString("ADDITIONALNOTES");
		this.decommissionReason = rs.getString("DECOMMISSIONREASON");
		this.decommissionDateComplete = rs.getDate("DECOMISSIONDATECOMPLETE");
		this.connectedRoomLabel = rs.getString("CONNECTEDROOM");
	}

	@Override
	protected boolean allowDuplicateRows() {
		return false;
	}	
	
	
	@Override
	public String toString(){
		return this.roomLabel;
	}
	

}
